package gui.view;

import generated.business.room.RoomService;
import generated.domain.avatar.Avatar;
import generated.domain.room.Room;
import gui.Activator;
import gui.IImageKeys;
import gui.ResourceManager;
import gui.action.PersistentMessageAction;
import gui.action.PrivateChatAction;
import gui.entity.AvatarEntity;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.part.ViewPart;

import vworld.Client;

public class RoomView extends ViewPart {
	public static final String ID = "gui.view.RoomView";

	private TreeViewer roomViewer;

	private PrivateChatAction privateChatAction;

	private PersistentMessageAction persistentMessageAction;

	private class RoomViewContentProvider implements
			IStructuredContentProvider, ITreeContentProvider {

		public Object[] getElements(Object inputElement) {
			List<AvatarEntity> entityList = new ArrayList<AvatarEntity>();
			for (Avatar a : ((RoomService) inputElement)
					.getRoomByName(Client.CURRENT_ROOM.name).insideAvatars) {
				entityList.add(new AvatarEntity(a));
			}
			return entityList.toArray(new AvatarEntity[entityList.size()]);
		}

		public void dispose() {
		}

		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}

		public Object[] getChildren(Object parentElement) {
			return null;
		}

		public Object getParent(Object element) {
			return null;
		}

		public boolean hasChildren(Object element) {
			if (element instanceof Room) {
				Room room = (Room) element;
				return (room.insideAvatars.length > 0);
			}
			return false;
		}
	}

	private class RoomViewLabelProvider extends LabelProvider implements
			IColorProvider {
		public String getText(final Object element) {
			if (element instanceof Room) {
				Room room = (Room) element;
				return (room.name);
			} else if (element instanceof AvatarEntity) {
				Avatar avatar = ((AvatarEntity) element).getAvatar();
				if (avatar.state) {
					return (avatar.name);
				} else {
					return (avatar.name + " (disconnected)");
				}
			}
			return "[NO_NAME_FOR_THIS_OBJECT]";
		}

		public Image getImage(final Object element) {
			if (element instanceof AvatarEntity) {
				Avatar avatar = ((AvatarEntity) element).getAvatar();
				if (Client.AVATAR_SERVICE.getUserRoleByAvatarName(avatar.name)
						.equalsIgnoreCase("administrator")) {
					return ResourceManager.getPluginImage(Activator
							.getDefault(), IImageKeys.ADMIN);
				}
				if (avatar.mood.equalsIgnoreCase("happy")) {
					return ResourceManager.getPluginImage(Activator
							.getDefault(), IImageKeys.HAPPY);
				}
				if (avatar.mood.equalsIgnoreCase("laughing")) {
					return ResourceManager.getPluginImage(Activator
							.getDefault(), IImageKeys.LAUGHING);
				}
				if (avatar.mood.equalsIgnoreCase("sad")) {
					return ResourceManager.getPluginImage(Activator
							.getDefault(), IImageKeys.SAD);
				}
				if (avatar.mood.equalsIgnoreCase("worried")) {
					return ResourceManager.getPluginImage(Activator
							.getDefault(), IImageKeys.WORRIED);
				}
				if (avatar.mood.equalsIgnoreCase("scared")) {
					return ResourceManager.getPluginImage(Activator
							.getDefault(), IImageKeys.SCARED);
				}
			}
			return null;
		}

		public Color getBackground(Object element) {
			return null;
		}

		public Color getForeground(Object element) {
			if (element instanceof AvatarEntity) {
				Avatar avatar = ((AvatarEntity) element).getAvatar();
				if (Client.AVATAR_SERVICE.getUserRoleByAvatarName(avatar.name)
						.equalsIgnoreCase("administrator")
						&& avatar.state) {
					return Display.getCurrent().getSystemColor(SWT.COLOR_RED);
				} else if (!avatar.state) {
					return Display.getCurrent().getSystemColor(SWT.COLOR_GRAY);
				}
			}
			return null;
		}
	}

	public void createPartControl(Composite parent) {
		roomViewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.BORDER);
		roomViewer.setContentProvider(new RoomViewContentProvider());
		roomViewer.setLabelProvider(new RoomViewLabelProvider());
		roomViewer.setInput(createModel());
		getSite().setSelectionProvider(roomViewer);
		makeActions();
		hookDoubleClickAction();
	}

	public void setFocus() {
		roomViewer.getControl().setFocus();
	}

	public Object createModel() {
		if (Client.CURRENT_ROOM != null) {
			return Client.ROOM_SERVICE;
		}
		return null;
	}

	private void makeActions() {
		privateChatAction = new PrivateChatAction(roomViewer);
		persistentMessageAction = new PersistentMessageAction(roomViewer);
	}

	private void hookDoubleClickAction() {
		roomViewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event
						.getSelection();
				if (!selection.isEmpty()) {
					Object object = selection.getFirstElement();
					if (object instanceof AvatarEntity) {
						Avatar avatar = ((AvatarEntity) object).getAvatar();
						if (avatar.state) {
							privateChatAction.run();
						} else {
							persistentMessageAction.run();
						}
					}
				}
			}
		});
	}

	public TreeViewer getRoomViewer() {
		return roomViewer;
	}

	public void changeTitle(String newTitle) {
		setPartName(newTitle);
	}
}